import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/gt_description_v3.dart';
import 'package:lib_uikit/widgets/gt_switch_v3.dart';

class GTCollapseWithSwitchV3 extends StatefulWidget {
  final List<GTCollapseWithSwitchModalV3>? list;
  final String? title;
  final String? descriptionTitle;
  final bool? isVisible;
  final ValueChanged<bool>? onClick;
  final String? subTitle;

  const GTCollapseWithSwitchV3(
      {super.key, this.list, this.title, this.descriptionTitle, this.isVisible, this.onClick, this.subTitle});

  @override
  State<StatefulWidget> createState() => _CollapseWithSwitchStateV3();
}

class _CollapseWithSwitchStateV3 extends State<GTCollapseWithSwitchV3> {
  late Color backgroundColor;
  late double paddingHorizontal;
  late bool isVisible;
  late List<GTCollapseWithSwitchModalV3> _list;
  bool _swicthValue=false;
  @override
  void initState() {
    isVisible = widget.isVisible ?? false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _list = widget.list??[];
    return Column(
      children: [
        GestureDetector(
          onTap: () {
            setState(() {
              isVisible = !isVisible;
              widget.onClick?.call(isVisible);
            });
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
            decoration: BoxDecoration(color: GTColorsV3.bg1.color),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      widget.title ?? "",
                      style: gtTextStyleW500(color: GTColorsV3.text1.color, fontSize: 16),
                    ),
                    if((widget.subTitle??"").isNotEmpty)...[
                      const SizedBox(
                        width: 4,
                      ),
                      Text(
                        widget.subTitle ?? "",
                        style: gtTextStyleW400(color: GTColorsV3.text3.color, fontSize: 11),
                      ),
                    ],
                  ],
                ),
                const SizedBox(
                  width: 12,
                ),
                Icon(
                  isVisible ? GTIconFont.e8c9 : GTIconFont.e8c8,
                  size: 16,
                  color: isVisible ? GTColorsV3.icon9.color : GTColorsV3.icon3.color,
                ),
              ],
            ),
          ),
        ),
        if (widget.list != null) ...[
          Visibility(
              visible: isVisible,
              child: Container(
                color: GTColorsV3.bg1.color,
                padding: const EdgeInsets.symmetric(horizontal: 12),
                child:_listWidget(context)
              ))
        ],
      ],
    );
  }

  Widget _listWidget(BuildContext context) {
    return MediaQuery.removePadding(
        context: context,
        removeTop: true,
        removeBottom: true,
        child: ListView.separated(
          separatorBuilder: (context, index) => const SizedBox(height: 12),
          itemCount: _list.length,
          shrinkWrap: true,
          itemBuilder: (context, index) {
            return _listItem(_list[index]);
          },
        ));
  }

  Widget _listItem(GTCollapseWithSwitchModalV3 data) {
    // _swicthValue = data.switchValue??false;
    return Row(
      children: [
        Expanded(child: Text(
          data.startText,
          style: gtTextStyleW400(fontSize: 14,color: GTColorsV3.text1.color),
        ),),
        const SizedBox(
          width: 12,
        ),
        if(data.showArrowIcon??false)...[
          Icon(GTIconFont.e89d,
          size: 16,
          color: GTColorsV3.icon3.color,),
        ],
        if(data.showSwitch??false)...[
          GTSwitchV3(
            switchValue: _swicthValue,
            onChanged: (value){
              setState(() {
                _swicthValue=!value;
              });
              data.switchChanged?.call(value);
            },
          )
        ],
      ],
    );
  }
}

class GTCollapseWithSwitchModalV3 {
  final String startText;
  final bool? showSwitch;
  final bool? showArrowIcon;
  final bool?switchValue;
  final ValueChanged<bool>?switchChanged;
  GTCollapseWithSwitchModalV3({required this.startText, this.showSwitch, this.showArrowIcon,this.switchValue,this.switchChanged});
}
